package cn.rubintry.bug.hunting.activity

import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.View
import android.view.ViewGroup
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.LinearLayoutManager
import cn.rubintry.bug.common.base.BaseActivity
import cn.rubintry.bug.hunting.R
import cn.rubintry.bug.hunting.adapter.LogListAdapter
import cn.rubintry.bug.hunting.databinding.ActivityMyLogBinding
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.SizeUtils
import com.yanzhenjie.recyclerview.OnItemMenuClickListener
import com.yanzhenjie.recyclerview.SwipeMenuCreator
import com.yanzhenjie.recyclerview.SwipeMenuItem
import java.io.File


class MyLogActivity : BaseActivity<ActivityMyLogBinding>(), View.OnClickListener {

    private var logAdapter: LogListAdapter? = null

    override fun bindingClazz(): Class<ActivityMyLogBinding> {
        return ActivityMyLogBinding::class.java
    }

    override fun getActivityParams() {

    }

    override fun initView() {
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        binding?.imgBack?.setOnClickListener(this)
        binding?.rvLogList?.layoutManager = LinearLayoutManager(this)
        binding?.rvLogList?.isNestedScrollingEnabled = false
        logAdapter = LogListAdapter()
        val swipeMenuCreator =
            SwipeMenuCreator { leftMenu, rightMenu, position ->
                val deleteItem =  SwipeMenuItem(this@MyLogActivity)
                deleteItem.setHeight(ViewGroup.LayoutParams.MATCH_PARENT)
                deleteItem.setWidth(SizeUtils.dp2px(80f))
                deleteItem.setTextColor(Color.WHITE)
                deleteItem.background = ColorDrawable(Color.parseColor("#ff4d4d"))
                deleteItem.text = "删除"
                rightMenu?.addMenuItem(deleteItem)
            }
        binding?.rvLogList?.setSwipeMenuCreator(swipeMenuCreator)
        // 菜单点击监听。
        val mItemMenuClickListener = OnItemMenuClickListener { menuBridge, position -> // 任何操作必须先关闭菜单，否则可能出现Item菜单打开状态错乱。
            menuBridge.closeMenu()

            // 左侧还是右侧菜单：
            val direction = menuBridge.direction
            // 菜单在Item中的Position：
            val menuPosition = menuBridge.position
            if(direction == -1 && menuPosition == 0){
                //删除
                val file = logAdapter?.getItem(position) as File
                file.delete()
                logAdapter?.removeAt(position)
            }
        }
        binding?.rvLogList?.setOnItemMenuClickListener(mItemMenuClickListener)

        binding?.rvLogList?.adapter = logAdapter

        logAdapter?.setOnItemClickListener { adapter, view, position ->
            val file = adapter.getItem(position) as File
            val intent = Intent(this , LogDetailActivity::class.java)
            intent.putExtra("filePath" , file.absolutePath)
            startActivity(intent)
        }
    }

    @SuppressLint("FileEndsWithExt")
    override fun observeData() {
        LogUtils.getLogFiles()?.forEach { file ->
            logAdapter?.add(file)
        }
    }

    override fun onNetWorkError(msg: String?) {

    }

    override fun onClick(v: View?) {
        when (v) {
            binding?.imgBack -> {
                finish()
            }
        }
    }

}